package com.zbkj.business.sys.reservation;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 考试预约
 * @create: 2025-03-25 15:20:54
 **/
@Service("examReservation")
@Slf4j
public class ExamReservationService{

    @Resource
    ExamReservationDao examReservationDao;

    /**
     * 添加考试预约,获取id数据
     *
     * @param cdoRequest - create_by:创建者
     *                   - create_date:创建时间
     *                   - del_flag:删除标记（0：正常；1：删除）
     *                   - exam_circumference:日期（周一到周日）
     *                   - exam_moon:日期时间（月）
     *                   - exam_name:姓名
     *                   - exam_phone:手机号
     *                   - exam_room:会议房间号
     *                   - exam_sun:日期时间（日）
     *                   - exam_time_slot_id:时间段id
     *                   - exam_time_slot_name:时间段（9-9：20等）
     *                   - exam_year:日期时间（年）
     *                   - id:编号
     *                   - remarks:备注信息
     *                   - update_by:更新者
     *                   - update_date:更新时间
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return addExamReservation(CDO cdoRequest) {
        examReservationDao.addExamReservation(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    @OpenApi
    public Return addExamReserva(CDO cdoRequest) {
        CDO  cdoExamReserva= new CDO();
        String exam_year = cdoRequest.getString("exam_year");
        String exam_room = cdoRequest.getString("exam_room");
        String remarks = cdoRequest.getString("remarks");
        CDO  cdoExamRemarks= new CDO();
        cdoExamRemarks.set("remarks",remarks);
        List<CDO> cdoExamRemarksList = examReservationDao.getExamReservationListByCondition(cdoExamRemarks);
        if (!cdoExamRemarksList.isEmpty()){
            return Return.valueOf(201,"您已参加过货已经报名预约完成！");
        }
        cdoExamReserva.set("exam_year",exam_year);
        cdoExamReserva.set("exam_room",exam_room);
        cdoExamReserva.set("exam_time_slot_id",cdoRequest.getString("exam_time_slot_id"));
        List<CDO> cdoExam = examReservationDao.getExamReservationListByCondition(cdoExamReserva);
        if (!cdoExam.isEmpty()){
            List<String> cdoE = new ArrayList<>();
            cdoE.add("09:00~09:20");
            cdoE.add("09:20~09:40");
            cdoE.add("09:40~10:00");
            cdoE.add("10:00~10:20");
            cdoE.add("10:20~10:40");
            cdoE.add("10:40~11:00");
            cdoE.add("11:00~11:20");
            cdoE.add("11:20~11:40");
            cdoE.add("11:40~12:00");
            cdoE.add("14:00~14:20");
            cdoE.add("14:20~14:40");
            cdoE.add("14:40~15:00");
            cdoE.add("15:00~15:20");
            cdoE.add("15:20~15:40");
            cdoE.add("15:40~16:00");
            cdoE.add("16:00~16:20");
            cdoE.add("16:20~16:40");
            cdoE.add("16:40~17:00");
            cdoE.add("17:00~17:20");
            cdoE.add("17:20~17:40");
            cdoE.add("17:40~18:00");
            cdoE.add("18:00~18:20");
            cdoE.add("18:20~18:40");
            cdoE.add("18:40~19:00");
            CDO  cdoExams= new CDO();
            cdoExams.set("exam_year",exam_year);
            cdoExams.set("exam_room",exam_room);
            List<CDO> cdoExamby = examReservationDao.getExamReservationListByCondition(cdoExamReserva);
            for (int i = 0; i <cdoExamby.size() ; i++) {
                cdoE.remove(cdoExamby.get(i).getInteger("exam_time_slot_id")-1);
            }
            CDO cdoResults = new CDO();
            cdoResults.set("cdoE", cdoE);
            return Return.valueOf(cdoResults);
        }else {
            examReservationDao.addExamReservation(cdoRequest);
            CDO cdoResult = new CDO();
            if (cdoRequest.containsKey("id")) {
                cdoResult.set("id", cdoRequest.get("id"));
            }
            return Return.valueOf(cdoResult);
        }
    }
    /**
     * 根据id，更新考试预约数据
     *
     * @param cdoRequest - create_by:创建者
     *                   - create_date:创建时间
     *                   - del_flag:删除标记（0：正常；1：删除）
     *                   - exam_circumference:日期（周一到周日）
     *                   - exam_moon:日期时间（月）
     *                   - exam_name:姓名
     *                   - exam_phone:手机号
     *                   - exam_room:会议房间号
     *                   - exam_sun:日期时间（日）
     *                   - exam_time_slot_id:时间段id
     *                   - exam_time_slot_name:时间段（9-9：20等）
     *                   - exam_year:日期时间（年）
     *                   - id:编号
     *                   - remarks:备注信息
     *                   - update_by:更新者
     *                   - update_date:更新时间
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return updateExamReservationById(CDO cdoRequest) {
        int affectedRows = examReservationDao.updateExamReservationById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除考试预约数据
     *
     * @param cdoRequest - id:编号
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    //@OpenApi
    public Return deleteExamReservationById(CDO cdoRequest){
        int affectedRows = examReservationDao.deleteExamReservationById(cdoRequest.getString("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除考试预约数据
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    //@OpenApi
    public Return deleteExamReservation(CDO cdoRequest) {
        int affectedRows = examReservationDao.deleteExamReservation(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取考试预约数据
     *
     * @param cdoRequest - id:编号
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return getExamReservationById(CDO cdoRequest) {
        CDO cdoMapperResult = examReservationDao.getExamReservationById(cdoRequest.getString("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取考试预约数据
     *
     * @param cdoRequest - id:编号
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return getExamReservation(CDO cdoRequest) {
        CDO cdoMapperResult = examReservationDao.getExamReservation(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取考试预约数据列表
     *
     * @param cdoRequest
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return getExamReservationList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = examReservationDao.getExamReservationList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = examReservationDao.getExamReservationListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取考试预约数据列表
     *
     * @param cdoRequest - id:编号
     * @return
     * @author 
     * @create: 2025-03-25 15:20:54
     */
    @OpenApi
    public Return getExamReservationListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = examReservationDao.getExamReservationListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = examReservationDao.getExamReservationListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
